package cn.aran.order.controller;

import cn.aran.basic.exception.BusinessException;
import cn.aran.basic.utils.LoginContext;
import cn.aran.order.service.IOrderProductService;
import cn.aran.order.domain.OrderProduct;
import cn.aran.order.query.OrderProductQuery;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import cn.aran.order.vo.HandleOrderVo;
import cn.aran.org.domain.Employee;
import cn.aran.org.service.IEmployeeService;
import cn.aran.pet.domain.SearchMasterMsg;
import cn.aran.pet.query.SearchMasterMsgQuery;
import cn.aran.pet.vo.HandlerMsgVo;
import cn.aran.user.domain.Logininfo;
import cn.aran.user.domain.User;
import cn.aran.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    public IOrderProductService orderProductService;

    @Autowired
    public IUserService userService;


    @Autowired
    public IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.save(orderProduct);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }


    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

	
    /**
    * 查询单个
    * @Parameters: [id]
    * @Return
    */
    @GetMapping("/{id}")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> list(){

        return orderProductService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderProduct> json(@RequestBody OrderProductQuery query)
    {
        return orderProductService.queryData(query);
    }

    /**
    * @Title: order
    * @Description: 提交订单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 13:20
    * @Parameters: [orderProduct, request]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/order")
    public AjaxResult order(@RequestBody OrderProduct orderProduct, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            User user = userService.loadByLogininfoId(logininfo.getId());
            orderProduct.setUserId(user.getId());
            orderProductService.order(orderProduct);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发布信息失败！" + e.getMessage());
        }
    }

    /**
    * @Title: toPay
    * @Description: 订单消息待支付
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 14:43
    * @Parameters: [query]
    * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
    */
    @PostMapping("/toPay")
    public PageList<OrderProduct> toPay(@RequestBody OrderProductQuery query) {
        // 设置订单消息为待支付
        query.setState(0);
        return orderProductService.queryData(query);
    }

    /**
    * @Title: toHandle
    * @Description: 待处理订单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 14:45
    * @Parameters: [query, request]
    * @Return cn.aran.basic.utils.PageList<cn.aran.order.domain.OrderProduct>
    */
    @PostMapping("/toHandle")
    public PageList<OrderProduct> toHandle(@RequestBody OrderProductQuery query, HttpServletRequest request) {
        // 设置寻主消息为待处理
        query.setState(1);
        // 设置店铺id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.loadByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        return orderProductService.queryData(query);
    }


    /**
    * @Title: pool
    * @Description: 订单池
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 14:46
    * @Parameters: [query]
    * @Return cn.aran.basic.utils.PageList<cn.aran.order.domain.OrderProduct>
    */
    @PostMapping("/pool")
    public PageList<OrderProduct> pool(@RequestBody SearchMasterMsgQuery query) {
        // 设置寻主消息为待处理
        query.setState(1);
        return orderProductService.queryData(query);
    }

    /**
     * @Title: finish
     * @Description: 查询已完成消息
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/7/20 15:11
     * @Parameters: [query]
     * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
     */
    @PostMapping("/finish")
    public PageList<OrderProduct> finish(@RequestBody OrderProductQuery query) {
        // 设置寻主消息为已完成
        query.setState(2);
        return orderProductService.queryData(query);
    }

    /**
     * @Title: userSearchMasterMsg
     * @Description: 前台用户根据状态查询发布的寻主消息
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/7/20 19:55
     * @Parameters: [query, request]
     * @Return cn.aran.basic.utils.PageList<cn.aran.pet.domain.SearchMasterMsg>
     */
    @PostMapping("/user")
    public PageList<OrderProduct> userOrderProduct(@RequestBody OrderProductQuery query, HttpServletRequest request) {
        // 1.设置当前登录用户Id
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userService.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        // 2.查询待审核的寻主消息寻主消息
        return  orderProductService.queryData(query);
    }

    /**
    * @Title: accept
    * @Description: 接受订单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 15:16
    * @Parameters: [handleOrderVo]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandleOrderVo handleOrderVo){
        try {
            orderProductService.accept(handleOrderVo);
            return AjaxResult.me();
        }catch(BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！！！");
        }
    }

    /**
    * @Title: reject
    * @Description: 拒绝订单
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/21 15:16
    * @Parameters: [msgId]
    * @Return cn.aran.basic.utils.AjaxResult
    */
    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId){
        try {
            orderProductService.reject(msgId);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }
}
